# -*- coding: utf-8 -*-
import redis


class ConnRedis:
    def __init__(self, host, port, db, pwd):
        self.host = str(host)
        self.port = int(port)
        self.db = int(db)
        self.pwd = str(pwd)

        self._conn = self.connRedis()

    def connRedis(self):
        redis_conn = False
        try:
            redis_conn = redis.Redis(host=self.host, port=self.port, db=self.db, password=self.pwd)
        except Exception, e:
            redis_conn = False
        # self._conn = redis_conn
        return redis_conn

    def getValue(self, key):
        flag = False
        if (self._conn):
            try:
                if self._conn.exists(self, key):
                    flag = self._conn.get(self, key)
                else:
                    flag = False
            except Exception, e:
                flag = False
        return flag

    def delkey(self, key):
        flag = False
        if (self._conn):
            try:
                self._conn.delete(*key)
                flag = True
            except Exception, e:
                flag = False
        return flag

    def getKeys(self, keys):
        flag = False
        if (self._conn):
            patternKeys = "*" + str(keys) + "*"
            print patternKeys
            try:
                flag = self._conn.keys(patternKeys)
            except Exception, e:
                flag = False
        return flag
